package com.lingx.service.query;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.lingx.model.IContext;
import com.lingx.model.IPerformer;
import com.lingx.model.impl.DefaultConfig;
import com.lingx.model.impl.DefaultEntity;
import com.lingx.service.ModelService;
import com.lingx.utils.Utils;
import com.lingx.web.api.IApi;

/** 
 * @author www.lingx.com
 * @version 创建时间：2015年4月26日 下午10:30:22 
 * 类说明 
 */
@Component
public class OrderCondition implements IGridCondition,ITreeCondition {
	@Resource
	private ModelService modelService;
	@Override
	public String getCondition(Map<String, Object> params,IContext context,IPerformer performer ) {
		String order = "desc";
		String sort = "id";
		StringBuilder sb=new StringBuilder();
		
		if("combo".equals(IApi.getParamString(params, "m", ""))){
			sort="convert("+this.modelService.getTextField(context.getEntity()).get(0)+" using gbk)";//convert(name using gbk)
			order="asc";
		}else if(Utils.isNotNull(IApi.getParamString(params, "orderField", ""))){//EXTJS的传参方式
			sort=IApi.getParamString(params, "orderField", "");//params.get("sort").toString();
			order=IApi.getParamString(params, "orderType", "");
		}else{//当Request没有传入排序参数时，从配置中读取
			DefaultEntity entity=context.getEntity();
			DefaultConfig gridConfig=entity.getConfigs().getList().get(0);
			sort=gridConfig.getSortName();
			order=gridConfig.getSortOrder();
			
		}
		sb.append(" order by ").append(sort).append(" ").append(order);
		return sb.toString();
	}
	

}
